﻿using System;
using Abs.Core.DAL;
using Abs.Core.DAL.AdoNet;
using Abs.Core.Models.Room;
using NUnit.Framework;

namespace Abs.Core.UnitTesting
{
    //[TestFixture]
    public class RoomScheduleRepository
    {
        private AdoNetRepository<RoomSchedule> AdoNetRepository { get; set; }
        private AdoNetRepository<Room> RoomAdoNetRepository { get; set; } 
        private RoomSchedule RoomSchedule { get; set; }
        private RoomSchedule ElementSchedule { get; set; }
        private Room Room { get; set; }
        private int DayId { get; set; }

        //[TestFixtureSetUp]
        public void BeforeTests()
        {
            this.RoomSchedule = new RoomSchedule
                                {
                                    RoomId = 1,
                                    DayId = 0,
                                    StartTime = DateTime.Parse("09:00:00"),
                                    EndTime = DateTime.Parse("17:00:00")
                                };

            this.Room = new Room { Name = "Element", RoomId = 6 };
            this.ElementSchedule = new RoomSchedule
                                {
                                    RoomId = this.Room.RoomId,
                                    DayId = 1,
                                    StartTime = DateTime.Parse("09:00:00"),
                                    EndTime = DateTime.Parse("10:00:00")
                                };
            this.DayId = (int) DayOfWeek.Tuesday;
        }//end of public void BeforeTests()

        //[SetUp]
        public void BeforeEachTest()
        {
            this.AdoNetRepository = new DAL.AdoNet.RoomScheduleRepository();
            this.RoomAdoNetRepository = new DAL.AdoNet.RoomRepository();
        }//end of internal void BeforeEachTest()

        //[TearDown]
        public void AfterEachTest()
        {
            if (this.AdoNetRepository != null)
                this.AdoNetRepository.Dispose();
            if(this.RoomAdoNetRepository != null)
                this.RoomAdoNetRepository.Dispose();
        }//end of internal void AfterEachTest()

        //[Test]
        public void CreateRoomSchedule()
        {
            var isSuccess = this.AdoNetRepository.Create(this.RoomSchedule);
            Assert.True(isSuccess);
        }//end of public void CreateRoomSchedule()

        //[Test]
        public void EditRoomSchedule()
        {
            this.RoomSchedule.StartTime = DateTime.Parse("08:00:00");
            var isSuccess = this.AdoNetRepository.Update(this.RoomSchedule);
            Assert.True(isSuccess);
        }//end of public void EditRoomSchedule()

        //[Test]
        public void GetRoomSchedule()
        {
            var id = new object[]
                     {
                         this.RoomSchedule.RoomId, 
                         this.RoomSchedule.DayId
                     };
            var room = this.AdoNetRepository.Select(id);
            Assert.NotNull(room);
        }//end of public void SelectRoomSchedule()

        //[Test]
        public void GetAllRoomSchedule()
        {
            var totalRooms = this.AdoNetRepository.SelectAll().Count;
            var expectedTotalRooms = 6;
            Assert.AreEqual(expectedTotalRooms, totalRooms);
        }//end of public void SelectAllRoomSchedule()

        //[Test]
        public void RemoveRoomSchedule()
        {
            var id = new object[]
                     {
                         this.RoomSchedule.RoomId, 
                         this.RoomSchedule.DayId
                     };
            var isSuccess = this.AdoNetRepository.Delete(id);
            Assert.True(isSuccess);
        }//end of public void RemoveRoomSchedule()

        //[Test]
        public void AddElementRoom()
        {
            var isSuccess = this.RoomAdoNetRepository.Create(this.Room);
            Assert.IsTrue(isSuccess);
        }//end of public void AddElementRoom()

        //[Test]
        public void CheckElementRoomIdentity()
        {
            var identity = ((IIdentity) this.RoomAdoNetRepository).Identity;
            Assert.AreEqual(this.Room.RoomId, identity);
        }//end of public void CheckElementRoomIdentity()

        //[Test]
        public void CreateElementRoomSchedule()
        {
            var isSuccess = this.AdoNetRepository.Create(this.ElementSchedule);
            Assert.True(isSuccess);
        }//end of public void AddElementRoomSchedule()

        //[Test]
        public void DeleteElementRoom()
        {
            var id = new object[] {this.Room.RoomId};
            var isSucces = this.RoomAdoNetRepository.Delete(id);
            id = new object[]
                 {
                     this.ElementSchedule.RoomId,
                     this.ElementSchedule.DayId
                 };
            var roomSchedule = this.AdoNetRepository.Select(id);
            Assert.True(isSucces);
            Assert.Null(roomSchedule);
        }//end of public void DeleteElementRoom()

        //[Test]
        public void GetRoomsOnDay()
        {
            var roomSchedules = ((DAL.AdoNet.RoomScheduleRepository) this.AdoNetRepository).GetRoomsOnDay(this.DayId);
            var totalRoomSchedules = roomSchedules.Count;
            var expectedTotalRoomSchedules = 3;
            Assert.AreEqual(expectedTotalRoomSchedules, totalRoomSchedules);
        }//end of public void GetRoomsOnDay()
    }//end of public class RoomScheduleRepository
}//end of namespace ABS.UnitTesting